package comp211.calendar.apptstorage;

import java.util.HashMap;
import java.util.Vector;

import comp211.calendar.unit.Appt;
import comp211.calendar.unit.TimeSpan;
import comp211.calendar.unit.User;

public class ApptStorageMemoryBasedImpl extends ApptStorage {

	public ApptStorageMemoryBasedImpl() {
		mAppts = new HashMap();
		User defaultUsertmp = new User("id", "password");
		defaultUser = defaultUsertmp;

		mAssignedApptID = 0;
	}

	public void RemoveAppt(Appt appt) {
		if (appt == null)
			return;
		mAppts.remove(appt.getID());
	}

	public Appt[] RetrieveAppts(TimeSpan d) {
		Appt temp;
		Vector v = new Vector(10, 1);
		for (int i = 1; i <= mAssignedApptID; i++) {
			temp = (Appt) mAppts.get(i);
			if (temp != null && d.Overlap(temp.TimeSpan()))
				v.add(temp);
		}
		if (v.size() == 0)
			return null;
		Appt[] result = new Appt[v.size()];
		for (int j = 0; j < result.length; j++)
			result[j] = (Appt) v.elementAt(j);
		return result;
	}

	public void SaveAppt(Appt appt) {
		mAssignedApptID++;
		appt.setID(mAssignedApptID);
		mAppts.put(mAssignedApptID, appt);
	}

	public Appt[] RetrieveAppts(User entity, TimeSpan time) {
		if (entity == null || time == null)
			return null;

		Vector v = new Vector(10, 1);
		Appt[] appts = RetrieveAppts(time);
		Appt[] result = null;
		if (appts != null) {
			for (int i = 0; i < appts.length; i++) {
				v.add(appts[i]);
			}

			if (v.size() == 0)
				return null;

			result = new Appt[v.size()];
			for (int j = 0; j < result.length; j++)
				result[j] = (Appt) v.elementAt(j);
		}

		return result;
	}

	public void UpdateAppt(Appt appt) {
		RemoveAppt(appt);
		SaveAppt(appt);
	}

	
	public User getDefaultUser() {
		return defaultUser;

	}

}
